package com.donger.common.pay.config;

import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.WxPayApiConfigKit;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@AllArgsConstructor
public class InitPay {

    private final WxBeanProperties wxBeanProperties;

    private final AliBeanProperties aliBeanProperties;


    /**
     * 微信支付配置文件
     * @return
     */
    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class})
    public WxPayApiConfig wxPayApiConfig(){
        WxPayApiConfig apiConfig;
        try {
            apiConfig = WxPayApiConfigKit.getApiConfig(wxBeanProperties.getAppId());
        } catch (Exception e) {
            apiConfig = WxPayApiConfig.builder()
                    .appId(wxBeanProperties.getAppId())
                    .mchId(wxBeanProperties.getMchId())
                    .partnerKey(wxBeanProperties.getPartnerKey())
                    .certPath(wxBeanProperties.getCertPath())
                    .domain(wxBeanProperties.getDomain())
                    .build();
        }
        wxBeanProperties.setNotifyUrl(apiConfig.getDomain().concat("/wxPay/payNotify"));
        wxBeanProperties.setRefundNotifyUrl(apiConfig.getDomain().concat("/wxPay/refundNotify"));
        return apiConfig;
    }


    /**
     * 阿里支付配置文件
     * @return
     */
    @SneakyThrows
    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class})
    public AliPayApiConfig aliPayApiConfig(){
        AliPayApiConfig aliPayApiConfig;
        try {
            aliPayApiConfig = AliPayApiConfigKit.getApiConfig(aliBeanProperties.getAppId());
        } catch (Exception e) {
            aliPayApiConfig = AliPayApiConfig.builder()
                    .setAppId(aliBeanProperties.getAppId())
                    .setAliPayPublicKey(aliBeanProperties.getPublicKey())
                    .setAppCertPath(aliBeanProperties.getAppCertPath())
                    .setAliPayCertPath(aliBeanProperties.getAliPayCertPath())
                    .setAliPayRootCertPath(aliBeanProperties.getAliPayRootCertPath())
                    .setCharset("UTF-8")
                    .setPrivateKey(aliBeanProperties.getPrivateKey())
                    .setServiceUrl(aliBeanProperties.getServerUrl())
                    .setSignType("RSA2")
                    // 普通公钥方式
                    //.build();
                    // 证书模式
                    .buildByCert();

        }
        return aliPayApiConfig;
    }


}
